package expression.v1.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import expression.v1.config.ExpConfig;
import expression.v1.model.ExpImage;
import expression.v1.model.ExpPkg;
import expression.v1.service.IExpImageService;
import expression.v1.service.IExpPackageService;

@Controller
@RequestMapping(value="/uploadfile/")
public class BatchFileUploadController {

	private static final String FILE_SEPARATOR = "/";
	
	@Autowired
	private ExpConfig expConfig;
	@Resource
	private IExpPackageService expPkgService;
	@Resource
	private IExpImageService expImgServiceImp;
	
	@RequestMapping(value = "uploadFiles")
	public String uploadFiles() {
		return "expmng/uploadfiles";
	}

	@ResponseBody
	@RequestMapping(value = "batchUpload", method = RequestMethod.POST)
	public String handleFileUpload(HttpServletRequest request) throws FileNotFoundException {
		List<MultipartFile> files = ((MultipartHttpServletRequest) request)
				.getFiles("Filedata");
		String keyword = request.getParameter("keyword");
		String pkgImg = request.getParameter("pkgImg");
		//ResourceUtils.getFile("classpath:/expressions/");
		File dir = new File(expConfig.getLocation() + keyword);
		if (!dir.exists()){
			dir.mkdir();
			System.out.println("-------->this is pkgImg:" + pkgImg);
			addPkg(keyword,keyword+FILE_SEPARATOR+pkgImg);
		}
		for (int i = 0; i < files.size(); i++) {
			MultipartFile file = files.get(i);
			String name = file.getOriginalFilename();
			if (!file.isEmpty()) {
				try {
					byte[] bytes = file.getBytes();
					File imgFile = new File(dir.getAbsolutePath() + FILE_SEPARATOR + name);
					FileOutputStream fos = new FileOutputStream(imgFile);
					fos.write(bytes);
					fos.close();
					addPkgImg(keyword,name,keyword+File.separator+name);
				} catch (Exception e) {
					return "You failed to upload " + name + " => "
							+ e.getMessage();
				}
			} else {
				return "You failed to upload " + name
						+ " because the file was empty.";
			}
		}
		return "upload successful";
	}
	
	private void addPkg(String keyword,String pkgImg){
		ExpPkg expPkg = new ExpPkg();
		expPkg.setId(UUID.randomUUID().toString());
		expPkg.setKeyword(keyword);
		expPkg.setPkgImg(pkgImg);
		expPkgService.addExpPackage(expPkg);
	}
	
	private void addPkgImg(String pkgName,String name,String imgPath){
		ExpImage expImg = new ExpImage();
		expImg.setId(UUID.randomUUID().toString());
		expImg.setName(name);
		expImg.setPath(imgPath);
		expImg.setPkg(pkgName);
		expImgServiceImp.addImage(expImg);
	}
}
